package com.gxwz.document.business.action;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.gxwz.document.business.entity.User;
import com.gxwz.document.business.entity.UserRole;
import com.gxwz.document.business.service.IuserRoleService;
import com.gxwz.document.core.action.BaseAction;

@SuppressWarnings("serial")
public class RoleAction extends BaseAction{
	protected static final String LIST_JSP = "/WEB-INF/page/roleManager/Role_list.jsp";
	protected static final String ADD_JSP = "/WEB-INF/page/roleManager/Role_add.jsp";
	protected static final String UPDATE_JSP = "/WEB-INF/page/roleManager/Role_update.jsp";
	
	private UserRole userRole;
	private List<UserRole> ListUserRole;
	private int totalRow  = 0;
	
	@SuppressWarnings("unused")
	@Autowired
	private IuserRoleService userRoleService;
	
    
	/**
	 * 跳转到角色列表页面
	 * @return
	 */
	public String openlist(){
		forwardView = LIST_JSP;
		ListUserRole = userRoleService.findByPage(0, 5);
		return SUCCESS;
	}
	
	/**
	 * 角色管理分页查询
	 */
	public String list()throws Exception{
		System.out.println("page:"+page);
		row = (page-1)*DEFUALT_PAGE_SIZE; 
		totalRow = userRoleService.getRowNum();
		if(page>(userRoleService.getRowNum()/DEFUALT_PAGE_SIZE+1)){
			page--;
		}
		if(row > userRoleService.getRowNum() || maxpage == 1){
			ListUserRole = userRoleService.findByPage((userRoleService.getRowNum()-(userRoleService.getRowNum()%DEFUALT_PAGE_SIZE)),DEFUALT_PAGE_SIZE);
		}else if(userRole != null){
			ListUserRole = userRoleService.findByOder(userRole, row, DEFUALT_PAGE_SIZE);
		}else{
			ListUserRole = userRoleService.findByPage(row,DEFUALT_PAGE_SIZE);
		}
		setForwardView(LIST_JSP);
		return SUCCESS;
	}
	/**
	 * 跳转到角色添加页面
	 * @return
	 */
	public String openAdd(){
		forwardView = ADD_JSP;
		return SUCCESS;
	}
	/**
	 * 角色添加
	 * @return
	 * @throws Exception 
	 */
	public String add() throws Exception{
		userRoleService.add(userRole);
		return list();
		}
	/**
	 * 跳转到角色修改页面
	 * @return
	 */
	public String openUpdate(){
		userRole=userRoleService.findById(userRole.getRoleId());
		forwardView = UPDATE_JSP;
		return SUCCESS;
	}
	/**
	 * 角色修改
	 * @return
	 * @throws Exception 
	 */
	public String update() throws Exception{
		userRoleService.update(userRole);
		return list();
		}
	/**
	 * 角色删除
	 */
	public String delete() throws Exception{
		userRoleService.delete(userRole.getRoleId());
		return list();
	}
	
	
	public List<UserRole> getListUserRole() {
		return ListUserRole;
	}

	public void setListUserRole(List<UserRole> ListUserRole) {
		this.ListUserRole = ListUserRole;
	}

	public int getTotalRow() {
		return totalRow;
	}

	public void setTotalRow(int totalRow) {
		this.totalRow = totalRow;
	}
	public UserRole getUserRole() {
		return userRole;
	}

	public void setUserRole(UserRole userRole) {
		this.userRole = userRole;
	}
}

